/**
 * 
 */
package com.acminds.fotoporter.ui.controllers;

import javax.faces.application.FacesMessage;
import javax.faces.application.FacesMessage.Severity;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;

import com.acminds.fotoporter.exceptions.AuthenticationFailureException;
import com.acminds.fotoporter.persistence.dto.UserInfo;
import com.acminds.fotoporter.service.client.UserInfoService;
import com.acminds.fotoporter.ui.BaseAction;
import com.acminds.fotoporter.utils.WebUtils;

/**
 * @author MANSUR
 *
 */
@ManagedBean
@RequestScoped
public class LoginAction extends BaseAction {

	private String userName;
	private String password;
	@ManagedProperty(value="#{userInfoService}")
	private UserInfoService userInfoService;
	
	public UserInfoService getUserInfoService() {
		return userInfoService;
	}
	public void setUserInfoService(UserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	public String login() {
		try {
			UserInfo userInfo = userInfoService.findUser(userName, password);
			WebUtils.setBeanToScope("authorizedUser", userInfo, true);
		} catch (AuthenticationFailureException e) {
			WebUtils.addMessage(FacesMessage.SEVERITY_ERROR, "invalidCredentials");
		}
		return null;
	}
	
	public String logout() {
		return null;
	}
	
}
